<?php
function getFieldHiddenFlag($field)
{
    if (getFieldCommonFlag($field)) {
        return 1;
    }
    return $field["primary_key"];
}

function getFieldCommonFlag($field)
{
    $temp = ",create_user,create_time,last_update_user,last_update_time,";
    if (strpos($temp, $field["column_name"]) > 0) {
        return 1;
    }
    return 0;
}


function getFieldLabelName($table, $field)
{
    if (getFieldCommonFlag($field)) {
        return "common." . $field["column_name"];
    }
    return $table . "." . $field["column_name"];
}

function getDicName($field)
{
    $comment = $field["column_comment"];
    if (strpos($comment, "dic:") === false) {
        return "";
    }
    $matrix = explode("dic:", $comment);
    return $matrix[1];
}

function getFieldEditHtml($field)
{
    $str = "";
    if ($field["hidden"] == 1) {
        return "";
    }

    if ($field["r_type"] == "textarea") {
        $str .= "<TEXTAREA id='" . $field["element_name"] . "' name='" . $field["element_name"] . "' ROWS=\"8\" COLS=\"72\"></TEXTAREA>";
    } else if ($field["r_type"] == "radio") {
        $str .= '<?= make_boolean_radio("' . $field["element_name"] . '","1") ?>{NEW_LINE}';
    } else if ($field["r_type"] == "select") {
        $tbl = str_replace("_id", "", $field["element_name"]);
        $nullableFlag = "false";
        if ($field["not_null"] == 0) {
            $nullableFlag = "true";
        }

        $str .= '<?php{NEW_LINE}';
        $str .= '{TAB}{TAB}{TAB}$sqlstr = \'select ' . $tbl . '_id,' . $tbl . '_name from ' . $tbl . '\';{NEW_LINE}';
        $str .= '{TAB}{TAB}{TAB}echo make_select("' . $field["element_name"] . '",$sqlstr,deep_get("' . $field["element_name"] . '",true,"",""),' . $field["not_null"] . ');{NEW_LINE}';
        $str .= '{TAB}{TAB}{TAB}?>{NEW_LINE}';
    } else {

        $name = "input";
        $attributes = array();
        $attributes["id"] = $field["element_name"];
        $attributes["name"] = $field["element_name"];

        if ($field["r_type"] == "date") {
            $attributes["label"] = $field["element_name"];
            $attributes["format"] = "yyyy-MM-dd";
            $attributes["init"] = 'current_time()';
            $str .= tag("itime", $attributes);
        } else {
            $attributes["id"] = $field["element_name"];
            $attributes["name"] = $field["element_name"];
            $attributes["size"] = 44;
            $str .= tag("input", $attributes);
        }

        $str .= "{NEW_LINE}";
    }

    $str = str_replace("{TAB}", "\t", $str);
    $str = str_replace("{NEW_LINE}", "\r\n", $str);
    return $str;
}

function getInsertHiddenColumns($field)
{
    $str = "";
    for ($i = 0; $i < count($field); $i++) {
        if ($field[$i] == "create_user") {
            $str .= '{TAB}<input type="hidden" name="create_user" value="<?=$sessionUser->user_id?>">{NEW_LINE}';
            continue;
        }
        if ($field[$i] == "create_time") {
            $str .= '{TAB}<input type="hidden" name="ignore_create_time" value="current_timestamp">{NEW_LINE}';
            continue;
        }
    }
    return $str;
}

function getUpdateHiddenColumns($field)
{
    $str = "";
    for ($i = 0; $i < count($field); $i++) {
        if ($field[$i] == "last_update_user") {
            $str .= '{TAB}<input type="hidden" name="last_update_user" value="<?=$sessionUser->user_id?>">{NEW_LINE}';
            continue;
        }
        if ($field[$i] == "last_update_time") {
            $str .= '{TAB}<input type="hidden" name="ignore_last_update_time" value="current_timestamp">{NEW_LINE}';
            continue;
        }
    }
    return $str;
}


function generate_view_table_html($tableName, $field, $field_name)
{
    $str = "";
    for ($i = 0; $i < count($field); $i++) {
        if (isIgnoreField($field[$i])) {
            continue;
        }
        $str .= "{TAB}<TR>{NEW_LINE}";
        $str .= "{TAB}{TAB}<TD class='col_label'>{NEW_LINE}";
        $str .= "{TAB}{TAB}{TAB}<?=label(\"" . $tableName . "." . $field[$i] . "\")?>{NEW_LINE}";
        $str .= "{TAB}{TAB}</TD>{NEW_LINE}";
        $str .= "{TAB}{TAB}<TD class='col_value'>{NEW_LINE}";
        $str .= "{TAB}{TAB}{TAB}";
        $str .= '<' . '?=html_replace($result[0]["' . $field[$i] . '"])?' . '>';

        $str .= "{NEW_LINE}{TAB}{TAB}</TD>{NEW_LINE}";
        $str .= "{TAB}</TR>{NEW_LINE}";
    }
    return $str;
}


function getColumnLen($list)
{
    $index = 0;
    for ($i = 0; $i < count($list); $i++) {
        if ($list[$i] == 1) {
            $index++;
            continue;
        }
    }
    return $index;
}

function generate_query_columns_html($field, $r_type)
{
    $str = "";
    for ($i = 0; $i < count($field); $i++) {
        if ($i > 0) {
            $str .= ",";
        }
        if ($r_type[$i] == "date") {
            $str .= "DATE_FORMAT(a." . $field[$i] . ",'%Y-%m-%d') as " . $field[$i];
        } else {
            $str .= "a." . $field[$i];
        }
    }
    // return $str ;
    return "*";
}

function getElementType($field, $type, $maxlength)
{
    if ($type == "int(11)" && strpos($field, "_id") != false) {
        return "select";
    }
    if ($type == "bit(1)") {
        return "radio";
    }
    if ($type == "datetime") {
        return "date";
    }
    if (strpos($field, "upload") != false || strpos($field, "file_name") != false) {
        return "upload";
    }
    if (preg_match("/text/", $type)) {
        return "textarea";
    }
    if (preg_match("/string/", $type) && $maxlength >= 200) {
        return "textarea";
    }
    if (preg_match("/decimal/", $type) || preg_match("/float/", $type)) {
        return "num";
    }
    if (preg_match("/int/", $type)) {
        return "int";
    }
    if (preg_match("/tel/", $field) || preg_match("/mobile/", $field) || preg_match("/phone/", $field)) {
        return "tel";
    }
    if (preg_match("/fax/", $field)) {
        return "fax";
    }
    if (preg_match("/email/", $field)) {
        return "email";
    }
    if (preg_match("/url/", $field) || preg_match("/website/", $field)) {
        return "url";
    }
    if (preg_match("/password/", $field) || preg_match("/_pass/", $field)) {
        return "password";
    }
    return "text";
}

function getRTYPE($field, $type)
{
    if (preg_match("/int/", $type)) {
        return "int";
    }
    if (preg_match("/text/", $type)) {
        return "textarea";
    }
    if (preg_match("/decimal/", $type) || preg_match("/float/", $type)) {
        return "num";
    }
    if (preg_match("/time/", $type) || preg_match("/float/", $type)) {
        return "date";
    }
    if (preg_match("/tel/", $field) || preg_match("/mobile/", $field) || preg_match("/phone/", $field)) {
        return "tel";
    }
    if (preg_match("/fax/", $field)) {
        return "fax";
    }
    if (preg_match("/email/", $field)) {
        return "email";
    }
    if (preg_match("/url/", $field) || preg_match("/website/", $field)) {
        return "url";
    }
    if (preg_match("/password/", $field) || preg_match("/_pass/", $field)) {
        return "password";
    }
    return "text";
}


function getFiledCss($type)
{
    if (preg_match("/int/", $type)) {
        return "col_r";
    }
    if (preg_match("/num/", $type)) {
        return "col_r";
    }
    if (preg_match("/date/", $type)) {
        return "col_r";
    }
    return "col_l";
}

function getRTYPEOptions($field, $type, $maxlength)
{
    $rtype = getElementType($field, $type, $maxlength);
    $types = array("text", "radio", "select", "entext", "int", "num", "textarea", "date", "tel", "fax", "email", "url", "password", "idcard");

    $str = "";
    $str .= "<SELECT NAME='r_type[]'>";

    for ($i = 0; $i < count($types); $i++) {
        $str .= "	<option value='" . $types[$i] . "'";
        if ($rtype == $types[$i]) {
            $str .= " selected";
        }
        $str .= ">" . $types[$i] . "</option>";
    }

    $str .= "</SELECT>";
    return $str;
}

function getFieldLength($type)
{
    $p1 = strpos($type, "(");
    if (!$p1) {
        return "";
    }
    $p2 = strpos($type, ")");
    //echo $p2 ;
    //echo
    return substr($type, $p1 + 1, $p2 - $p1 - 1);
}

function isIgnoreField($fieldName)
{
    $names = array("create_user", "create_time", "last_update_user", "last_update_time");
    for ($i = 0; $i < count($names); $i++) {
        if ($fieldName == $names[$i]) {
            return true;
        }
    }
    return false;
}

function getLabels($table, $field, $field_name, $remark)
{
    $s = "";
    for ($i = 0; $i < count($field_name); $i++) {
        $fname = $field_name[$i];
        $fieldName = $field[$i];
        if (isIgnoreField($fieldName)) {
            continue;
        }
        $s .= '$labels["' . $table . '.' . $fieldName . '"] = "' . $fname . '";{NEW_LINE}';
        $s .= '$labels["' . $table . '.' . $fieldName . '.help"] = "' . $remark[$i] . '";{NEW_LINE}';
    }
    return $s;
}

